package com.lagou.config;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import com.lagou.annotion.Autowired;
import com.lagou.annotion.Comppent;
import com.lagou.factory.BeanFactory;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;

import java.io.InputStream;

/**
 * @Authther: mrwu
 * @date:2021/3/12-03
 * @Description:com.lagou.config
 */
@Comppent
public class Config {
    //sqlsession构建器
    @Autowired
    private  MybatisSqlSessionFactoryBuilder sqlSessionFactoryBuilder;

    public void setSqlSessionFactoryBuilder(MybatisSqlSessionFactoryBuilder factoryBuilder) {
        this.sqlSessionFactoryBuilder = factoryBuilder;
    }

    private static SqlSession sqlSession;
//   static  {
//        try {
//            InputStream inputStream = Resources.getResourceAsStream("sqlMapconfig.xml");
////            MybatisSqlSessionFactoryBuilder factoryBuilder = new MybatisSqlSessionFactoryBuilder();
//            sqlSession = sqlSessionFactoryBuilder.build(inputStream).openSession();//根据需要初始化是可以设置是否自动提交
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
    public Config() {

    }

    public SqlSession getSqlSession() {
        try {
            InputStream inputStream = Resources.getResourceAsStream("sqlMapconfig.xml");
//            MybatisSqlSessionFactoryBuilder factoryBuilder = new MybatisSqlSessionFactoryBuilder();
            sqlSession = sqlSessionFactoryBuilder.build(inputStream).openSession();//根据需要初始化是可以设置是否自动提交
        }catch (Exception e){
            e.printStackTrace();
        }
        return sqlSession;
    }
}
